package com.jcl.cloud.services.crawler.brain.service;

import com.jcl.cloud.common.service.exception.JclCrawlException;
import com.jcl.cloud.services.brain.common.dto.CrawlDispatchTask;
import com.jcl.cloud.services.catchemail.client.CatchEmailClient;
import com.jcl.cloud.services.catchnew.client.CatchNewClient;
import com.jcl.cloud.services.crawler.brain.constant.TaskRecord;
import com.jcl.cloud.services.crawler.brain.constant.TaskType;
import com.jcl.cloud.services.login.client.LoginWorkerClient;
import com.jcl.cloud.services.search.client.SearchWorkerClient;
import com.jcl.cloud.services.unicrawler.common.dto.GrabResult;
import org.springframework.beans.factory.annotation.Autowired;

public abstract class BaseGrabWorkerService {

    @Autowired
    private RedisService redisService;

    @Autowired
    private LoginWorkerClient loginWorkerClient;
    @Autowired
    private CatchNewClient catchNewClient;
    @Autowired
    private SearchWorkerClient searchWorkerClient;
    @Autowired
    private CatchEmailClient catchEmailClient;



    protected abstract void afterWorker(CrawlDispatchTask crawlDispatchTask,Object object);


    public void doWorker(CrawlDispatchTask crawlDispatchTask,Object object) throws JclCrawlException {

        try {
            if(crawlDispatchTask.isNeedWork()){
                String taskType = crawlDispatchTask.getTaskType();

                if(taskType.equals(TaskType.LOGIN.toString())){
                    this.loginWorkerClient.createTask(crawlDispatchTask.getCrawlerId());


                }


                if(taskType.equals(TaskType.SEARCH.toString())){
                  this.searchWorkerClient.createTask(crawlDispatchTask.getCrawlerId());
                }


                if(taskType.equals(TaskType.CATCH_NEW.toString())){
                    this.catchNewClient.createTask(crawlDispatchTask.getCrawlerId());
                }


                if(taskType.equals(TaskType.CATCH_EMAIL.toString())){
                    this.catchEmailClient.createTask(crawlDispatchTask.getCrawlerId());
                }

                System.out.println("===========taskType = " + taskType);
            }
        } catch (Exception e) {
            //发生异常将任务返还回队列
            this.redisService.lpush("unicrawler:tasks:all",crawlDispatchTask.getCrawlerId());
        }
    }



    protected void beforeWorker(CrawlDispatchTask crawlDispatchTask, Object object) throws JclCrawlException {
        //进行数据转换、判断、处理
        GrabResult grabResult = (GrabResult)object;
        long currentTime = System.currentTimeMillis();
        long endTime = Long.valueOf(grabResult.getCron().split("-")[1]);
        long nextExecuteTime = Long.valueOf(grabResult.getNextExecuteTime());

        String crawlerId = grabResult.getCrawlerId();
        crawlDispatchTask.setCrawlerId(crawlerId);

        //TaskRecord.taskRecordMap.put(crawlerId,currentTime);





        //判断是否超过结束时间
        if(endTime > currentTime){
            //如果还没到结束时间
            if(nextExecuteTime < currentTime){
                //如果下次执行时间已经到了，则执行任务
                String taskType = this.redisService.hget("unicrawler:task:info:"+crawlerId,"taskType");
                String siteCode = this.redisService.hget("unicrawler:task:info:"+crawlerId,"siteCode");

                crawlDispatchTask.setNeedWork(true);
                crawlDispatchTask.setTaskType(taskType);


                //设置下一次的任务类型
                if(taskType.equals(TaskType.LOGIN.toString())){


                    this.redisService.hset("unicrawler:task:info:"+crawlerId,"taskType",TaskType.SEARCH.toString());
                }

                if(taskType.equals(TaskType.SEARCH.toString())){
                    this.redisService.hset("unicrawler:task:info:"+crawlerId,"taskType",TaskType.CATCH_NEW.toString());
                }

                if(taskType.equals(TaskType.CATCH_NEW.toString())){
                    this.redisService.hset("unicrawler:task:info:"+crawlerId,"taskType",TaskType.CATCH_EMAIL.toString());
                }


                if(taskType.equals(TaskType.CATCH_EMAIL.toString())){
                    this.redisService.hset("unicrawler:task:info:"+crawlerId,"taskType",TaskType.SEARCH.toString());
                }


                //根据siteCode设置下次执行时间

                //this.redisService.hset("unicrawler:task:info:"+crawlerId,"nextExecuteTime",String.valueOf(currentTime));

            }else {
                //如果下次执行时间还没到，则将任务扔回队列
                crawlDispatchTask.setNeedWork(false);
                this.redisService.lpush("unicrawler:tasks:all",crawlerId);
            }

        }else {
            //如果到了结束时间
            if(grabResult.isNeedSearch()){
                //如果需要进行搜索,则继续进行搜索任务，但是不删除爬虫任务

                if(nextExecuteTime < currentTime){
                    crawlDispatchTask.setNeedWork(true);
                    crawlDispatchTask.setTaskType(TaskType.SEARCH.toString());
                    this.redisService.hset("unicrawler:task:info:"+crawlerId,"taskType",TaskType.SEARCH.toString());
                    nextExecuteTime = currentTime + 30;
                    this.redisService.hset("unicrawler:task:info:"+crawlerId,"nextExecuteTime",String.valueOf(nextExecuteTime));

                }else {
                    //扔回队列
                    crawlDispatchTask.setNeedWork(false);
                    this.redisService.lpush("unicrawler:tasks:all",crawlerId);
                }



            }else {
                //如果不需要进行搜索，则删除爬虫任务
                crawlDispatchTask.setNeedWork(false);
                this.redisService.del(grabResult.getCrawlerId());
            }
        }
    }


}
